package com.sinoglobal.sinostore.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.lidroid.xutils.util.LogUtils;
import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.activity.GoodsDetailActivity;
import com.sinoglobal.sinostore.utils.TextUtil;
/**
 * 
 * @title: GoodsWebFragment.java 
 * @package com.chiliring.sinostore.fragment 
 * @description: TODO(图文详情) 
 * @author LK
 * @date 2015-1-23 下午3:13:54
 */
public final class GoodsWebFragment extends Fragment implements OnCheckedChangeListener {
	private boolean isVisible = false;

	//	@ViewInject(R.id.rg_goods_info)
	private RadioGroup radioGroup;

	//	@ViewInject(R.id.rb_goodsintroduce)
	private RadioButton rbGoodsintroduce;

	//	@ViewInject(R.id.rb_businessesintroduce)
	private RadioButton rbBusinessesintroduce;

	//	@ViewInject(R.id.vp_graphic)
	private ViewPager viewPager;

	private MyPagerAdapter pagerAdapter;

	public static GoodsWebFragment newInstance() {
		GoodsWebFragment fragment = null;
		if(null == fragment){
			fragment = new GoodsWebFragment();
		}
		return fragment;
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View layout=inflater.inflate(R.layout.shop_fragment_goods_web, null);
		//		ViewUtils.inject(this,layout);
		initView(layout);
		//防止被该view 覆盖的view 响应touch事件
		layout.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				v.onTouchEvent(event);
				return true;
			}
		});
		initData();
		LogUtils.e("GoodsWebFragment.onCreateView");
		LogUtils.d("GoodsWebFragment.onCreateView");
		return layout;
	}

	@Override
	public void setUserVisibleHint(boolean isVisibleToUser) {
		super.setUserVisibleHint(isVisibleToUser);
		LogUtils.e("setUserVisibleHint isVisibleToUser=="+isVisibleToUser);
		LogUtils.e("setUserVisibleHint getUserVisibleHint()=="+getUserVisibleHint());
		LogUtils.e("setUserVisibleHint isVisible=="+isVisible);
		if(getUserVisibleHint()) {
			if(!isVisible){
				initData();
				isVisible = true;
			}
		} 
	}

	private void initView(View view){
		//		@ViewInject(R.id.rg_goods_info)
		radioGroup=getViewById(view,R.id.rg_goods_info);

		rbGoodsintroduce=getViewById(view,R.id.rb_goodsintroduce);

		rbBusinessesintroduce=getViewById(view,R.id.rb_businessesintroduce);

		viewPager=getViewById(view,R.id.vp_graphic);
		radioGroup.setOnCheckedChangeListener(this);
	}
	protected <T extends View>  T getViewById(View view, int id) {
		return (T) view.findViewById(id);
	}
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		LogUtils.e("onResume");
		//		initData();
	}
	private void initData(){
		LogUtils.e("initData");
		//		pagerAdapter = new MyPagerAdapter(getFragmentManager());
		if(pagerAdapter==null
				&&TextUtil.stringIsNotNull(GoodsDetailActivity.goodsId)
				&&TextUtil.stringIsNotNull(GoodsDetailActivity.shopId)
				){
			pagerAdapter = new MyPagerAdapter(getChildFragmentManager());
			GoodsDescFragment goodsInfo = GoodsDescFragment.newInstance(1,GoodsDetailActivity.goodsId);
			pagerAdapter.AddFragment(goodsInfo);
			GoodsDescFragment businessesintroduce = GoodsDescFragment.newInstance(2,GoodsDetailActivity.shopId);
			pagerAdapter.AddFragment(businessesintroduce);
		}
		viewPager.setAdapter(pagerAdapter);
		//		viewPager.setCurrentItem(0);
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				switch (position) {
				case 0:
					rbGoodsintroduce.setChecked(true);
					break;
				case 1:
					rbBusinessesintroduce.setChecked(true);
					break;
				default:
					break;
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
	}



	private class MyPagerAdapter extends FragmentPagerAdapter{
		List<Fragment> list;  
		public MyPagerAdapter(FragmentManager fm) {  
			super(fm);  
			list = new ArrayList<Fragment>();
		}

		public void AddFragment(Fragment fragment){
			if(null != fragment){
				list.add(fragment);
			}
			notifyDataSetChanged();
		}

		@Override  
		public int getCount() {  
			return list.size();  
		}  

		@Override  
		public Fragment getItem(int arg0) {  
			return list.get(arg0);  
		}  
	}



	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		int checkedRadioButtonId = group.getCheckedRadioButtonId();
		if (checkedRadioButtonId == R.id.rb_goodsintroduce) {
			viewPager.setCurrentItem(0);
		} else if (checkedRadioButtonId == R.id.rb_businessesintroduce) {
			viewPager.setCurrentItem(1);
		} else {
		}
	}
}
